package com.demo.jfinal.job;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.core.sgip.client.SGIP2DownClient;
import com.demo.jfinal.model.ApiDownstream;
import com.demo.jfinal.model.SmsOrders;
import com.jfinal.plugin.activerecord.Db;

/**
 * 向客户推送状态报告
 * @author Guoxing
 *
 */
public class Request2DownPushstateJob implements Job {
	private static Logger log = Logger.getLogger(Request2DownPushstateJob.class);
	/*
	 * 从表里拿数据向下游推送状态
	 */
	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

		log.info("向下游推送状态报告任务正在执行：" + System.currentTimeMillis());
		Date date = new Date();
		// 获取当前数据

		List<SmsOrders> smsOrdersList = SmsOrders.dao
				.find("SELECT T.* FROM SMS_ORDERS T WHERE 1=1 AND T.IS_DOWN_PUSH = 'N' and T.DATE_TYPE = '1' and T.LAST_SEND_DATE <= ?", date);

		if (smsOrdersList == null || smsOrdersList.isEmpty()) {
			log.info("Request2DownPushstateJob无数据处理");
			return;
		}else{
			 Db.batch("update sms_orders set IS_DOWN_PUSH = 'Y' where 1=1 and ID = ? ", "ID", smsOrdersList, smsOrdersList.size());
		}
		List<String> listPhone = new ArrayList<String>();
		// 根据客户的配置信息 拿到 客户提供的sgip服务，去推送
		SmsOrders smsOrders = new SmsOrders();
		for (int i = 0; i < smsOrdersList.size(); i++) {
			smsOrders = smsOrdersList.get(i);
			String phoneNo = smsOrders.getStr("PHONE_NO");
			log.info("正在处理数据：phoneno = " + phoneNo + ",orderId = " + smsOrders.getStr("ORDER_ID"));
			listPhone.add(phoneNo);
			String customerNo = smsOrders.getStr("CUSTOMER_NO");
			String lastState = smsOrders.getStr("LAST_STATE");
			String lastDesc = smsOrders.getStr("LAST_DESC");
			Date lastSendDate = smsOrders.getDate("LAST_SEND_DATE");
			String content = smsOrders.getStr("CONTENT");

			// 拿到客户信息 ，去回调状态给对应的客户
			// String confId = smsOrders.getStr("CONF_ID");
			int confId = smsOrders.getInt("CONF_ID");

			ApiDownstream apiDownstream = ApiDownstream.dao.getApiDownstreamCacheById(String.valueOf(confId));
			if (apiDownstream == null) {
				apiDownstream = ApiDownstream.dao.findFirst(
						"select * from sms_api_downstream_config c where 1=1 and PROTOCOL_TYPE = '1' and CONF_ID = ?",
						confId);
			}

			if (apiDownstream == null) {
				log.info("向下游推送状态报告时未获取到ID=" + confId + "的客户配置信息");
				continue;
			}

			// 客户ip：port
			String ipportkeypas = apiDownstream.getStr("STATE_POST");

			log.info("向下游推送状态报告参数conf_id = " + confId + ",ipportkeypas = " + ipportkeypas);
			// String appkey = apiDownstream.getStr("APPKEY");
			// String password = apiDownstream.getStr("SECURTY");
			String ip = ipportkeypas.split(":")[0];
			String port = ipportkeypas.split(":")[1];
			String appkey = ipportkeypas.split(":")[2];
			String password = ipportkeypas.split(":")[3];

			try {
				SGIP2DownClient.sendMsgPushstate(ip, port, appkey, password, listPhone, content, customerNo, lastState,
						lastDesc, lastSendDate);
			} catch (Exception e) {
				e.printStackTrace();
			}

//			Db.update("update SMS_ORDERS t set t.IS_DOWN_PUSH = 'Y' where 1=1 and t.LAST_SEND_DATE <= ? ", date);

		}

	}

}









